package ui.troopmanager.mooresr.us;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import troopmanager.mooresr.us.DataManager;
import troopmanager.mooresr.us.Person;

import com.toedter.calendar.JDateChooser;

public class PeopleDetailDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private Integer personId;
	private Boolean isNew;

	private JLabel firstNameLabel = new JLabel("First Name:");
	private JTextField firstNameText = new JTextField(30);
	private JLabel lastNameLabel = new JLabel("Last Name:");
	private JTextField lastNameText = new JTextField(30);
	private JLabel typeLabel = new JLabel("Type:");
	private JComboBox typeComboBox = new JComboBox();
	private JRadioButton maleButton = new JRadioButton("Male");
	private JRadioButton femaleButton = new JRadioButton("Female");
	private ButtonGroup genderGroup = new ButtonGroup();
	private JLabel driversLicenseLabel = new JLabel("Drivers License:");
	private JTextField driversLicenseText = new JTextField(10);
	private JLabel dobLabel = new JLabel("Date of Birth:");
	private JDateChooser dobChooser = new JDateChooser();
	private JLabel medFormALabel = new JLabel("Health History From received:");
	private JDateChooser medFormAChooser = new JDateChooser();
	private JButton copyFromFormBButton = new JButton("Copy from Form B");
	private JLabel medFormBLabel = new JLabel("Health Exam Form received:");
	private JDateChooser medFormBChooser = new JDateChooser();
	private JLabel medFormCLabel = new JLabel("Hold Harmless received:");
	private JDateChooser medFormCChooser = new JDateChooser();
	private JLabel cellPhoneLabel = new JLabel("Cell Phone:");
	private JTextField cellPhoneText = new JTextField(16);
	private JLabel homePhoneLabel = new JLabel("Home Phone:");
	private JTextField homePhoneText = new JTextField(16);	
	private JLabel workPhoneLabel = new JLabel("Work Phone:");
	private JTextField workPhoneText = new JTextField(16);
	private JLabel emailLabel = new JLabel("Email:");
	private JTextField emailText = new JTextField(30);
	private JLabel activeLabel = new JLabel("Active:");
	private JCheckBox activeCheckBox = new JCheckBox();
	private JButton okButton = new JButton("OK");
	private JButton cancelButton = new JButton("Cancel");
	
	public PeopleDetailDialog() {
		initComponents();
	}
	
	private void initComponents() {
		GridBagLayout layout = new GridBagLayout();
		
		setTitle("Edit/Add Person");
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(Color.white);
		setForeground(Color.black);

		setLayout(layout);
		
		// Constraints used for the labels in the first column
		GridBagConstraints labelConstraints = new GridBagConstraints();
		labelConstraints.gridx = 0;
		labelConstraints.gridy = GridBagConstraints.RELATIVE;  // Next row
		labelConstraints.anchor = GridBagConstraints.LINE_END;
		labelConstraints.ipadx = 16;
		
		// Constraints for (most of) the controls in the second column
		GridBagConstraints controlConstraints = new GridBagConstraints();
		controlConstraints.gridx = 1;
		controlConstraints.gridy = GridBagConstraints.RELATIVE;  // Next row
		controlConstraints.anchor = GridBagConstraints.LINE_START;
		controlConstraints.ipadx = 16;
		
		// Constraints for wide controls in the second column
		GridBagConstraints wideControlConstraints = new GridBagConstraints();
		wideControlConstraints.gridx = 1;
		wideControlConstraints.gridy = GridBagConstraints.RELATIVE;  // Next row
		wideControlConstraints.anchor = GridBagConstraints.LINE_START;
		wideControlConstraints.ipadx = 16;
		wideControlConstraints.gridwidth = 2;
		
		// Constraints for controls in the third column
		GridBagConstraints thirdColumnControlConstraints = new GridBagConstraints();
		thirdColumnControlConstraints.gridx = 2;
		thirdColumnControlConstraints.gridy = GridBagConstraints.RELATIVE;  // Next row
		thirdColumnControlConstraints.anchor = GridBagConstraints.LINE_START;
		thirdColumnControlConstraints.ipadx = 16;
		
		add(firstNameLabel, labelConstraints);

		add(firstNameText, wideControlConstraints);

		add(lastNameLabel, labelConstraints);

		add(lastNameText, wideControlConstraints);

		add(typeLabel, labelConstraints);

		typeComboBox.addItem("Adult");
		typeComboBox.addItem("Scout");
		add(typeComboBox, controlConstraints);
		
		add(Box.createVerticalGlue(), thirdColumnControlConstraints);
		
		genderGroup.add(maleButton);
		genderGroup.add(femaleButton);
		
		add(Box.createVerticalGlue(), labelConstraints);

		add(maleButton, controlConstraints);
		
		add(Box.createVerticalGlue(), thirdColumnControlConstraints);
		
		add(Box.createVerticalGlue(), labelConstraints);
		
		add(femaleButton, controlConstraints);
		
		add(Box.createVerticalGlue(), thirdColumnControlConstraints);

		add(driversLicenseLabel, labelConstraints);

		add(driversLicenseText, controlConstraints);
		
		add(Box.createVerticalGlue(), thirdColumnControlConstraints);
		
		add(dobLabel, labelConstraints);

		add(dobChooser, controlConstraints);
		
		add(Box.createVerticalGlue(), thirdColumnControlConstraints);
		
		add(medFormALabel, labelConstraints);

		add(medFormAChooser, controlConstraints);

		add(copyFromFormBButton, thirdColumnControlConstraints);

		add(medFormCLabel, labelConstraints);

		add(medFormCChooser, controlConstraints);
		
		add(medFormBLabel, labelConstraints);

		add(medFormBChooser, controlConstraints);

		add(cellPhoneLabel, labelConstraints);

		add(cellPhoneText, wideControlConstraints);
		
		add(homePhoneLabel, labelConstraints);

		add(homePhoneText, wideControlConstraints);
		
		add(workPhoneLabel, labelConstraints);

		add(workPhoneText, wideControlConstraints);
		
		add(emailLabel, labelConstraints);
		
		add(emailText, wideControlConstraints);
		
		add(activeLabel, labelConstraints);

		add(activeCheckBox, controlConstraints);

		add(okButton, labelConstraints);

		add(cancelButton, controlConstraints);
		
		setSize(600, 400);
		setModal(true);
		
		okButton.addActionListener(new ActionListener() {			
			public void actionPerformed(ActionEvent event) {
				okButtonCallback(event);
			}
		});
		
		cancelButton.addActionListener(new ActionListener() {		
			public void actionPerformed(ActionEvent event) {
				cancelButtonCallback(event);
			}
		});
		
		copyFromFormBButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				copyFromFormBButtonCallback(event);
			}
		});
	}
	

	
	private void okButtonCallback(ActionEvent event) {
		Integer type = typeComboBox.getSelectedIndex() + 1;
		Date formA = null;
		Date formB = null;
		Date formC = null;
		Date dob = null;
		Boolean active;
		
		// TODO:  Validate the data
		
		/* All the gyrations with the dates are because DateChooser
		 * returns java.util.Date and I'm working with java.sql.Date
		 * so I need to convert them.
		 */
		
		if (medFormAChooser.getDate() != null) {
			formA = new Date(medFormAChooser.getDate().getTime());
		}
		if (medFormBChooser.getDate() != null) {
			formB = new Date(medFormBChooser.getDate().getTime());
		}
		if (medFormCChooser.getDate() != null) {
			formC = new Date(medFormCChooser.getDate().getTime());
		}
		
		if (firstNameText.getText().length() == 0) {
			JOptionPane.showMessageDialog(null, "First name can not be blank.", "Data Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		if (lastNameText.getText().length() == 0) {
			JOptionPane.showMessageDialog(null, "Last name can not be blank.", "Data Error", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		active = activeCheckBox.isSelected();
		
		if (dobChooser.getDate() != null) {
			dob = new Date(dobChooser.getDate().getTime());
		}
		
		String gender;
		if (maleButton.isSelected()) {
			gender="M";
		} else {
			gender = "F";
		}
		
		Person p = new Person(
				personId,
				firstNameText.getText(),
				lastNameText.getText(),
				type,
				formA,
				formB,
				formC,
				driversLicenseText.getText(),
				active,
				dob,
				cellPhoneText.getText(),
				homePhoneText.getText(),
				workPhoneText.getText(),
				gender,
				emailText.getText());
		
		if (isNew) {
			DataManager.getInstance().addPerson(p);
		} else {
			DataManager.getInstance().updatePerson(p);
		}
		setVisible(false);
	}
	
	private void cancelButtonCallback(ActionEvent event) {
		setVisible(false);
	}
	
	private void copyFromFormBButtonCallback(ActionEvent event) {
		
		medFormAChooser.setDate(medFormBChooser.getDate());
		
	}
	
	public void loadData(Person p) {
		if (p == null) {
			firstNameText.setText("");
			lastNameText.setText("");
			driversLicenseText.setText("");
			medFormAChooser.setDate(null);
			medFormBChooser.setDate(null);
			medFormCChooser.setDate(null);
			activeCheckBox.setSelected(true);
			dobChooser.setDate(null);
			cellPhoneText.setText("");
			homePhoneText.setText("");
			workPhoneText.setText("");
			emailText.setText("");
			maleButton.setSelected(true);
			femaleButton.setSelected(false);
			isNew = true;
			personId = 0;
		} else {
			firstNameText.setText(p.getFirstName());
			lastNameText.setText(p.getLastName());
			driversLicenseText.setText(p.getDriversLicense());
			if (p.getType() == 1) {
				typeComboBox.setSelectedItem("Adult");
				driversLicenseText.setEditable(true);
			} else {
				typeComboBox.setSelectedItem("Scout");
				driversLicenseText.setEditable(false);
			}
			
			medFormAChooser.setDate(p.getMedFormA());
			medFormBChooser.setDate(p.getMedFormB());
			medFormCChooser.setDate(p.getMedFormC());
			dobChooser.setDate(p.getDob());
			cellPhoneText.setText(p.getCellPhone());
			workPhoneText.setText(p.getWorkPhone());
			homePhoneText.setText(p.getHomePhone());
			emailText.setText(p.getEmail());
			
			activeCheckBox.setSelected(p.getActive());
			
			if (p.getGender().equals("M")) {
				maleButton.setSelected(true);
				femaleButton.setSelected(false);	
			} else {
				maleButton.setSelected(false);
				femaleButton.setSelected(true);
			}
			
			isNew = false;
			personId = p.getPersonId();
		}
		
	}

}
